const util = {
    type: obj => Object.prototype.toString.call( obj ).slice( 8, -1 ).toLowerCase(),
    isPlainObject: obj => util.type( obj ) === "object",
    isFunction: func => util.type( func ) === "function",
    isRegExp: reg => util.type( reg ) === "regexp",
    appendToBody: el => document.body.appendChild( el ),
    createDiv: () => document.createElement( "div" ),
    getCss: ( el, prop ) => window.getComputedStyle( el, null ).getPropertyValue( prop ),
    clone ( obj ) {
        let newObj = null;
        if ( typeof obj !== "object" || obj === null || obj === undefined ) {
            newObj = obj;
        } else if ( Array.isArray( obj ) ) {
            newObj = [];
            obj.forEach(item => {
                newObj.push( util.clone( item ) )
            })
        } else if ( util.isPlainObject( obj ) ) {
            newObj = {};
            for ( const i in obj ) {
                if ( Array.isArray( obj[ i ] ) || util.isPlainObject( obj ) ) {
                    newObj[ i ] = util.clone( obj[ i ] );
                } else {
                    newObj[ i ] = obj[ i ];
                }
            }
        } else {
            newObj = obj;
        }
        return newObj;
    },
    delay ( duration = 0 ) {
        return new Promise(resolve => {
            const timer = window.setTimeout(() => {
                window.clearTimeout( timer );
                resolve();
            }, duration)
        })
    },
    lacpPublicOpen ( instance, type, data ) {
        if ( util.isPlainObject( data ) ) {
            instance.containerIsShow = true;
            instance.mode = type;
            for ( const key in data ) {
                const value = data[ key ];
                if ( util.isPlainObject( value ) ) {
                    for ( const _key in value ) {
                        instance[ type ][ key ][ _key ] = value[ _key ];
                    }
                } else {
                    instance[ type ][ key ] = value;
                }
            }
            util.delay().then(() => {
                instance.layerIsShow = true;
            })
        }
    },
    lacpPublicClose ( instance ) {
        instance.layerIsShow = false;
    }
};

export default util;